/*
 *  Copyright (c) 2010-2023
 */
package net.siufung.market.base.uuid.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.siufung.core.model.enums.DeletedEnum;
import net.siufung.core.page.PagerResult;
import net.siufung.core.result.Result;
import net.siufung.core.result.ResultFail;
import net.siufung.core.result.ResultOk;
import net.siufung.core.utils.CopyUtil;
import net.siufung.market.base.uuid.controller.assembler.SysUuidAssembler;
import net.siufung.market.base.uuid.controller.model.SysUuidModel;
import net.siufung.market.base.uuid.controller.regulation.SysUuidRegulation;
import net.siufung.market.base.uuid.mapper.ISysUuidMapper;
import net.siufung.market.base.uuid.mapper.entity.SysUuid;
import net.siufung.market.base.uuid.service.ISysUuidService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * 流水号表 前端控制层
 *
 * @author 陈建峰
 * @since 2023-10-09 22:16:17
 * @version 1.0.0
 */
@Api(tags = "流水号表")
@Slf4j
@AllArgsConstructor
@RestController
@RequestMapping("/sys/uuid")
public class SysUuidController {

    private final ISysUuidService sysUuidService;
    private final ISysUuidMapper sysUuidMapper;

    /**
     * 通过主键获取 流水号表
     * @param id 主键
     * @return Result<SysUuidModel.Vo>
     * */
    @ApiOperation(value = "通过Id获取流水号表")
    @GetMapping(value = "/id/{id}")
    public Result<SysUuidModel.Vo> getById(
            @PathVariable("id") Integer id) {
        SysUuid sysUuid = sysUuidMapper.selectById(id);
        if(Objects.nonNull(sysUuid)){
            return Result.ok(SysUuidAssembler.from(sysUuid));
        }
        return Result.fail();
    }

    /**
     * 通过主键删除 流水号表 对象
     * @param id 主键
     * @return Result<Void>
     * */
    @ApiOperation(value = "通过主键删除流水号表对象")
    @PostMapping(value="/remove/{id}")
    public Result<Void> removeById(
            @PathVariable("id") Integer id) {
        SysUuidRegulation.Remove sysUuidRegulationRemove = new SysUuidRegulation.Remove(sysUuidMapper);
        sysUuidRegulationRemove.isSatisfiedBy(id);
        if(sysUuidService.removeById(id)){
            return Result.ok(ResultOk.REMOVE);
        }
        return Result.fail(ResultFail.DATA_REMOVE_ERROR);
    }

    /**
     * 通过主键集合批量删除 流水号表
     * @param ids 主键集合
     * @return Result<Void>
     * */
    @ApiOperation(value = "通过主键集合批量删除流水号表对象")
    @PostMapping(value="/removeByIds")
    public Result<Void> removeByIds(
            @RequestBody Integer[] ids) {
        if(sysUuidService.removeByIds(Arrays.asList(ids))){
            return Result.ok(ResultOk.REMOVE);
        }
        return Result.fail(ResultFail.DATA_REMOVE_ERROR);
    }

    /**
     * 通过主键更新 流水号表 信息
     * @param sysUuidUpdateDto 对象属性
     * @return Result<Void>
     * */
    @ApiOperation(value = "更新 流水号表 信息")
    @PostMapping(value="/updateById")
    public Result<Void> updateById(
            @RequestBody SysUuidModel.UpdateDto sysUuidUpdateDto) {
        sysUuidUpdateDto.validated();
        SysUuidRegulation.Update sysUuidRegulationUpdate = new SysUuidRegulation.Update(sysUuidMapper);
        sysUuidRegulationUpdate.isSatisfiedBy(sysUuidUpdateDto);
        SysUuid sysUuidOf = sysUuidMapper.selectById(sysUuidUpdateDto.getId());
        CopyUtil.copyPropertiesIgnoreNull(sysUuidUpdateDto.to(), sysUuidOf);
        if(sysUuidService.updateById(sysUuidOf)){
            return Result.ok(ResultOk.UPDATE);
        }
        return Result.fail(ResultFail.DATA_UPDATE_ERROR);
    }

    /**
     * 更新 流水号表 状态
     * @param id 主键
     * @param deleted 数据状态
     * @return Result<Void>
     * */
    @ApiOperation(value = "更新 流水号表 状态")
    @PostMapping(value="/status/{id}/{deleted}")
    public Result<Void> updateStatusById(
            @PathVariable("id") Integer id,
            @PathVariable("deleted") boolean deleted) {
        SysUuidRegulation.Status sysUuidRegulationStatus = new SysUuidRegulation.Status(sysUuidMapper);
        sysUuidRegulationStatus.isSatisfiedBy(id);
        if(sysUuidService.updateStatusById(id, DeletedEnum.get(deleted))){
            return Result.ok(ResultOk.STATUS);
        }
        return Result.fail(ResultFail.DATA_STATUS_ERROR);
    }

    /**
     * 获取 流水号表 分页
     * @param sysUuidPageDto 属性
     * @return Result<IPager<SysUuidModel.Vo>>
     * */
    @ApiOperation(value = "获取 流水号表 分页")
    @PostMapping(value="/page")
    public Result<PagerResult<SysUuidModel.Vo>> getPage(
            @RequestBody SysUuidModel.PageDto sysUuidPageDto) {
        sysUuidPageDto.validated();
        QueryWrapper<SysUuid> wrapper = new QueryWrapper<>();
        wrapper.lambda().orderByDesc(SysUuid::getId);
        IPage<SysUuid> sysUuidPage = sysUuidMapper.selectPage(
                new Page<>(sysUuidPageDto.getPageNo(), sysUuidPageDto.getPageSize()), wrapper);
        List<SysUuid> sysUuidList = sysUuidPage.getRecords();
        PagerResult<SysUuidModel.Vo> sysUuidVoPage = new PagerResult<>(
                sysUuidPage.getCurrent(), sysUuidPage.getSize(), sysUuidPage.getTotal());
        sysUuidVoPage.setRecords(
                SysUuidAssembler.fromList(sysUuidList));
        return Result.ok(sysUuidVoPage);
    }

    /**
     * 获取 流水号表 列表
     * @param sysUuidDto 属性
     * @return Result<List<SysUuidModel.Vo>>
     * */
    @ApiOperation(value = "获取流水号表列表")
    @PostMapping(value = "/list")
    public Result<List<SysUuidModel.Vo>> list(
            @RequestBody SysUuidModel.Dto sysUuidDto) {
        List<SysUuid> sysUuidList = sysUuidMapper.selectList(Wrappers.query(sysUuidDto.to()));
        return Result.ok(SysUuidAssembler.fromList(sysUuidList));
    }

    /**
     * 创建 流水号表
     * @param sysUuidSaveDto 对象属性
     * @return Result<Void>
     * */
    @ApiOperation(value = "创建 流水号表")
    @PostMapping(value="/save")
    public Result<Void> save(
            @RequestBody SysUuidModel.SaveDto sysUuidSaveDto) {
        sysUuidSaveDto.validated();
        SysUuidRegulation.Save sysUuidRegulationSave = new SysUuidRegulation.Save(sysUuidMapper);
        sysUuidRegulationSave.isSatisfiedBy(sysUuidSaveDto);
        if(sysUuidService.save(sysUuidSaveDto.to())){
            return Result.ok(ResultOk.ADD);
        }
        return Result.fail(ResultFail.DATA_ADD_ERROR);
    }

}